package com.imooc.utils;

import com.sun.tools.internal.jxc.ap.Const;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Arrays;

// 用于生成缩略图
@Component
public class ImageUtils  {
    /**
     *
     * @param in1
     *            文件流
     *
     *
     * @param wide
     *            宽
     * @param high
     *            高
     * @return
     * @throws IOException
     */

    // 输入inputstream ，进行图片的裁剪，返回 inputstream
    public static InputStream thumbnailImage(InputStream in1,  int wide, int high) throws IOException {
        InputStream inThumb = null;

        String types = Arrays.toString(ImageIO.getReaderFormatNames()).replace("]", ",");
        String suffix = null;

        Image img = ImageIO.read(in1);
        BufferedImage bi = new BufferedImage(wide, high, BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.drawImage(img, 0, 0, wide, high, Color.LIGHT_GRAY, null);
        g.dispose();
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imOut;
        imOut = ImageIO.createImageOutputStream(bs);
        ImageIO.write(bi, "jpg", imOut);
        inThumb = new ByteArrayInputStream(bs.toByteArray());
        return inThumb;
    }

    // 根据输入的字符串创建文件
    private static  File createFile(String fileName){
        return new File(fileName);
    }

    //base64字符串转化成图片file
    public static File base64ToFile(String base64, String fileName) throws Exception {
        if(base64.contains("data:image")){
            base64 = base64.substring(base64.indexOf(",")+1);
        }
        base64 = base64.toString().replace("\r\n", "");
        File file = null;
        //创建文件目录

        BufferedOutputStream bos = null;
        java.io.FileOutputStream fos = null;
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bytes =  decoder.decodeBuffer(base64);

            file= createFile(fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        }finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }

    // 将 inputstream转为file
    public  static  File inputstreamtofile(InputStream ins,String fileName) throws IOException {

        File file = createFile(fileName);

        OutputStream os = new FileOutputStream(file);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();

        return file ;
    }

}
